package com.hp.gxw.api;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.hp.gxw.constant.R;
import com.hp.gxw.entity.*;
import com.hp.gxw.service.ISaleOrderDetailsService;
import com.hp.gxw.service.ISaleOrderProcessService;
import com.hp.gxw.service.ISaleOrderService;
import com.hp.gxw.service.ISettlementOrderService;
import com.hp.gxw.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
@RequestMapping("/api/dispatchList")
public class DispatchListApi {

    @Autowired
    private ISettlementOrderService iSettlementOrderService;

    @Autowired
    private ISaleOrderService iSaleOrderService;

    @Autowired
    private ISaleOrderDetailsService iSaleOrderDetailsService;

    @Autowired
    private ISaleOrderProcessService iSaleOrderProcessService;

    /**
     * 结算单数据接收
     * @param map
     * @return
     */
    @RequestMapping("/register")
    @ResponseBody
    public R somain_register(@RequestBody Map<String, Object> map){
        if(map == null){
            return R.error("参数为空");
        }
        SettlementOrder settlementOrder = new SettlementOrder();
        settlementOrder.setStatus(false);
        if(map.get("customerorder") != null && StringUtils.isNotEmpty(map.get("customerorder").toString())){
            settlementOrder.setCustomerOrder(map.get("customerorder").toString());
        }
        if(map.get("orderno") == null){
            return R.error("orderno为必填项");
        }
        settlementOrder.setOrderNo(map.get("orderno").toString());
        if(map.get("relatedorder") != null && StringUtils.isNotEmpty(map.get("relatedorder").toString())){
            settlementOrder.setRelatedOrder(map.get("relatedorder").toString());
        }
        if(map.get("ccuscode") == null){
            return R.error("ccuscode为必填项");
        }
        settlementOrder.setCustomerCode(map.get("ccuscode").toString());
        if(map.get("ccusname") == null){
            return R.error("ccusname为必填项");
        }
        settlementOrder.setCustomerName(map.get("ccusname").toString());
        if(map.get("saleOrderNo") == null){
            return R.error("saleOrderNo为必填项");
        }
        settlementOrder.setSaleOrderNo(map.get("saleOrderNo").toString());
        SaleOrder saleOrder = iSaleOrderService.getOne(new LambdaQueryWrapper<SaleOrder>().eq(SaleOrder::getOrderNo, map.get("saleOrderNo").toString()));
        if(saleOrder == null){
            return R.error("传递的销售订单号："+map.get("saleOrderNo")+",不存在，请确认");
        }
        SettlementOrder one = iSettlementOrderService.getOne(new LambdaQueryWrapper<SettlementOrder>().eq(SettlementOrder::getOrderNo, map.get("orderno")));
        if(one != null){
            return R.error("结算单号："+map.get("orderno")+",已存在! 请勿重复传递");
        }
        if(map.get("personname") != null && StringUtils.isNotEmpty(map.get("personname").toString())){
            settlementOrder.setPersonName(map.get("personname").toString());
        }
        if(map.get("relatedOrder") != null && StringUtils.isNotEmpty(map.get("relatedOrder").toString())){
            settlementOrder.setRelatedOrder(map.get("relatedOrder").toString());
        }
        if(map.get("ddate") != null && StringUtils.isNotEmpty(map.get("ddate").toString())){
            settlementOrder.setCreateTime(DateUtil.getDateTimeFormat(map.get("ddate").toString()));
        }else{
            settlementOrder.setCreateTime(new Date());
        }
        if(map.get("cstcode") == null){
            return R.error("cstcode为必填项");
        }
        settlementOrder.setCstcode(map.get("cstcode").toString());
        if(map.get("cdepcode") == null){
            return R.error("cdepcode为必填项");
        }
        settlementOrder.setCdepcode(map.get("cdepcode").toString());

        if(map.get("cls") != null){
            settlementOrder.setCls(map.get("cls").toString());
        }
        if(map.get("cplb") != null){
            settlementOrder.setCplb(map.get("cplb").toString());
        }
        if(map.get("ddsx") != null){
            settlementOrder.setDdsx(map.get("ddsx").toString());
        }

        if(map.get("details") == null){
            return R.error("表体详情数据不能为空");
        }

        List<SettlementOrderDetails> settlementOrderDetailsList = JSON.parseArray(JSON.toJSON(map.get("details")).toString(), SettlementOrderDetails.class);
        for(int i=0; i<settlementOrderDetailsList.size(); i++){
//            if(settlementOrderDetailsList.get(i).getDetailId() == null){
//                return R.error("表体详情数据第"+(i+1)+"行detailId为空");
//            }
            if(settlementOrderDetailsList.get(i).getCinvcode() == null || StringUtils.isEmpty(settlementOrderDetailsList.get(i).getCinvcode())){
                return R.error("表体详情数据第"+(i+1)+"行cinvcode为空");
            }
//            if(settlementOrderDetailsList.get(i).getCbatch() == null || StringUtils.isEmpty(settlementOrderDetailsList.get(i).getCbatch())){
//                return R.error("表体详情数据第"+(i+1)+"行cbatch为空");
//            }
            if(settlementOrderDetailsList.get(i).getCinvname() == null || StringUtils.isEmpty(settlementOrderDetailsList.get(i).getCinvname())){
                return R.error("表体详情数据第"+(i+1)+"行cinvname为空");
            }
            if(settlementOrderDetailsList.get(i).getType() == null || StringUtils.isEmpty(settlementOrderDetailsList.get(i).getType())){
                return R.error("表体详情数据第"+(i+1)+"行type为空");
            }
            if(settlementOrderDetailsList.get(i).getSettlementType() == null || StringUtils.isEmpty(settlementOrderDetailsList.get(i).getSettlementType())){
                return R.error("表体详情数据第"+(i+1)+"行settlementType为空");
            }
            if(settlementOrderDetailsList.get(i).getProcess() == null || settlementOrderDetailsList.get(i).getProcess().size() < 1){
                return R.error("表体详情数据第"+(i+1)+"行process订单工序数据为空");
            }
            if(settlementOrderDetailsList.get(i).getSfsw() == null){
                return R.error("表体详情数据第"+(i+1)+"行sfsw为空");
            }
            if(settlementOrderDetailsList.get(i).getSfll() == null){
                return R.error("表体详情数据第"+(i+1)+"行sfll为空");
            }
            if(settlementOrderDetailsList.get(i).getLlrq() == null){
                return R.error("表体详情数据第"+(i+1)+"行llrq为空");
            }
            if(settlementOrderDetailsList.get(i).getCzjslx() == null){
                return R.error("表体详情数据第"+(i+1)+"行czjslx为空");
            }
//            if(settlementOrderDetailsList.get(i).getSblx() == null){
//                return R.error("表体详情数据第"+(i+1)+"行sblx为空");
//            }
            if(settlementOrderDetailsList.get(i).getTsbz() == null){
                return R.error("表体详情数据第"+(i+1)+"行tsbz为空");
            }
            if(settlementOrderDetailsList.get(i).getLv() == null){
                return R.error("表体详情数据第"+(i+1)+"行lv为空");
            }
            settlementOrderDetailsList.get(i).setCompletFlag(false);
            SaleOrderDetails orderDetails = null;
            try {
                orderDetails = iSaleOrderDetailsService.getOne(new LambdaQueryWrapper<SaleOrderDetails>()
                        .eq(SaleOrderDetails::getMasterId, saleOrder.getId())
                        .eq(SaleOrderDetails::getMesId, settlementOrderDetailsList.get(i).getDetailId()));
                if (null == orderDetails){
                    return R.error("销售订单号："+saleOrder.getOrderNo()+"与本地数据不对称！需要删除当前销售订单进行重传销售订单！");
                }
            }catch (Exception e){
                e.printStackTrace();
                return R.error("销售订单号："+saleOrder.getOrderNo()+"表体中存在相同的detail_id");
            }
            for(int j=0; j<settlementOrderDetailsList.get(i).getProcess().size(); j++){
                if(settlementOrderDetailsList.get(i).getProcess().get(j).getSerialNumber() == null){
                    return R.error("表体订单工序数据第"+(j+1)+"行serialNumber为空");
                }
                if(settlementOrderDetailsList.get(i).getProcess().get(j).getProductProcess() == null || StringUtils.isEmpty(settlementOrderDetailsList.get(i).getProcess().get(j).getProductProcess())){
                    return R.error("表体订单工序数据第"+(j+1)+"行productProcess为空");
                }
                if(settlementOrderDetailsList.get(i).getProcess().get(j).getProductProcessName() == null || StringUtils.isEmpty(settlementOrderDetailsList.get(i).getProcess().get(j).getProductProcessName())){
                    return R.error("表体订单工序数据第"+(j+1)+"行productProcessName为空");
                }
                if(settlementOrderDetailsList.get(i).getProcess().get(j).getOrderProcess() == null || StringUtils.isEmpty(settlementOrderDetailsList.get(i).getProcess().get(j).getOrderProcess())){
                    return R.error("表体订单工序数据第"+(j+1)+"行orderProcess为空");
                }
                if(settlementOrderDetailsList.get(i).getProcess().get(j).getOrderProcessName() == null || StringUtils.isEmpty(settlementOrderDetailsList.get(i).getProcess().get(j).getOrderProcessName())){
                    return R.error("表体订单工序数据第"+(j+1)+"行orderProcessName为空");
                }
                if(settlementOrderDetailsList.get(i).getProcess().get(j).getQty() == null || settlementOrderDetailsList.get(i).getProcess().get(j).getQty().compareTo(BigDecimal.ZERO) == 0){
                    return R.error("表体订单工序数据第"+(i+1)+"行qty为空或者为0");
                }
                if(settlementOrderDetailsList.get(i).getProcess().get(j).getExpectHours() == null){
                    return R.error("表体订单工序数据第"+(i+1)+"行expectHours为空");
                }
                if(settlementOrderDetailsList.get(i).getProcess().get(j).getUnQty() == null){
                    return R.error("表体订单工序数据第"+(i+1)+"行unQty为空");
                }
                List<SaleOrderProcess> list = iSaleOrderProcessService.list(new LambdaQueryWrapper<SaleOrderProcess>()
                        .eq(SaleOrderProcess::getDetailsId, orderDetails.getMesId())
                        .eq(SaleOrderProcess::getOrderProcess, settlementOrderDetailsList.get(i).getProcess().get(j).getOrderProcess()));
                if(list.size() < 1){
                    return R.error("表体详情数据第"+(i+1)+"行订单工序与销售订单号："+saleOrder.getOrderNo()+"对应的工序匹配不上");
                }
            }
        }
        try {
            iSettlementOrderService.saveInfo(settlementOrder, settlementOrderDetailsList);
        }catch (Exception e){
            e.printStackTrace();
            return R.error("接口异常");
        }
        return R.ok("写入成功");
    }
}
